package com.ruoyi.msg.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.msg.domain.bo.MsgFeedbackBo;
import com.ruoyi.msg.domain.vo.MsgFeedbackVo;
import com.ruoyi.msg.service.IMsgFeedbackService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * 意见反馈
 *
 * @author strongme
 * @date 2023-04-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/msg/feedback")
public class MsgFeedbackController extends BaseController {

    private final IMsgFeedbackService iMsgFeedbackService;

    /**
     * 查询意见反馈列表
     */
    @SaCheckPermission("msg:feedback:list")
    @GetMapping("/list")
    public TableDataInfo<MsgFeedbackVo> list(MsgFeedbackBo bo, PageQuery pageQuery) {
        return iMsgFeedbackService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出意见反馈列表
     */
    @SaCheckPermission("msg:feedback:export")
    @Log(title = "意见反馈", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MsgFeedbackBo bo, HttpServletResponse response) {
        List<MsgFeedbackVo> list = iMsgFeedbackService.queryList(bo);
        ExcelUtil.exportExcel(list, "意见反馈", MsgFeedbackVo.class, response);
    }

    /**
     * 获取意见反馈详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("msg:feedback:query")
    @GetMapping("/{id}")
    public R<MsgFeedbackVo> getInfo(@NotNull(message = "主键不能为空")
                                    @PathVariable Long id) {
        return R.ok(iMsgFeedbackService.queryById(id));
    }

    /**
     * 新增意见反馈
     */
    @SaCheckPermission("msg:feedback:add")
    @Log(title = "意见反馈", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MsgFeedbackBo bo) {
        if (bo.getUserId() == null) {
            bo.setUserId(LoginHelper.getUserId());
        }
        return toAjax(iMsgFeedbackService.insertByBo(bo));
    }

    /**
     * 修改意见反馈
     */
    @SaCheckPermission("msg:feedback:edit")
    @Log(title = "意见反馈", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MsgFeedbackBo bo) {
        return toAjax(iMsgFeedbackService.updateByBo(bo));
    }

    /**
     * 删除意见反馈
     *
     * @param ids 主键串
     */
    @SaCheckPermission("msg:feedback:remove")
    @Log(title = "意见反馈", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iMsgFeedbackService.deleteWithValidByIds(Arrays.asList(ids), true));
    }
}
